import { ref } from "vue"
import store from "@/store"
import { defineStore } from "pinia"
import { usePermissionStore } from "./permission"
import { useTagsViewStore } from "./tags-view"
import { useSettingsStore } from "./settings"
import { useLargeModelStore } from "./large-model"
import { getCookie, getToken, removeToken, setCookie, setToken, removeCookie } from "@/utils/cache/cookies"
import router, { resetRouter } from "@/router"
import { loginApi, getUserInfoApi } from "@/api/login"
import { type LoginRequestData } from "@/api/login/types/login"
import { type RouteRecordRaw } from "vue-router"
import routeSettings from "@/config/route"
import { getLargeModelToken } from "@/api/large-model"

export const useUserStore = defineStore("user", () => {
  const token = ref<string>(getToken() || "")
  const roles = ref<string[]>([])
  const username = ref<string>("")
  const permissionStore = usePermissionStore()
  const tagsViewStore = useTagsViewStore()
  const settingsStore = useSettingsStore()
  const cookie = getCookie("userinfo") || "{}"
  const userinfo = ref<any>(JSON.parse(cookie))
  const largeModelStore = useLargeModelStore()
  /** 设置角色数组 */
  const setRoles = (value: string[]) => {
    roles.value = value
  }
  /** 登录 */
  const login = async (params: LoginRequestData) => {
    const { data } = await loginApi({ account: params.account, password: params.password, code: params.code })
    setToken(data.token)
    token.value = data.token
    console.log(data.userInfo)
    setCookie("userinfo", JSON.stringify(data.userInfo))
    userinfo.value = data.userInfo
    return data
  }
  /** 获取用户详情 */
  const getInfo = async () => {
    // const { data } = await getUserInfoApi(userinfo.value.id)
    const { data } = await getLargeModelToken()
    setCookie("large-model-token", data)
    largeModelStore.SET_LARGE_MODEL_TOKEN(data)
    roles.value = routeSettings.defaultRoles
  }
  /** 切换角色 */
  const changeRoles = async (role: string) => {
    const newToken = "token-" + role
    token.value = newToken
    setToken(newToken)
    await getInfo()
    permissionStore.setRoutes(roles.value)
    resetRouter()
    permissionStore.dynamicRoutes.forEach((item: RouteRecordRaw) => {
      router.addRoute(item)
    })
    _resetTagsView()
  }
  /** 登出 */
  const logout = () => {
    removeToken()
    removeCookie("userinfo")
    token.value = ""
    roles.value = []
    resetRouter()
    _resetTagsView()
  }
  /** 重置 Token */
  const resetToken = () => {
    removeToken()
    token.value = ""
    roles.value = []
  }
  /** 重置 Visited Views 和 Cached Views */
  const _resetTagsView = () => {
    if (!settingsStore.cacheTagsView) {
      tagsViewStore.delAllVisitedViews()
      tagsViewStore.delAllCachedViews()
    }
  }

  return {
    token,
    roles,
    username,
    userinfo,
    largeModelStore,
    setRoles,
    login,
    getInfo,
    changeRoles,
    logout,
    resetToken
  }
})

/** 在 setup 外使用 */
export function useUserStoreHook() {
  return useUserStore(store)
}
